﻿using Setme.Domain.Repository;
using Setme.Domain.UnitOfWork;
using Setme.Domain.UnitOfWork.EntityFramework;
using Setme.Infrastructure;
using System;
using System.Linq;

namespace Setme.Domain.Service
{
    public class EmployeeService : IEmployeeService
    {
        public OperationResult CreateEmployee(Model.Employee model)
        {
            using (IUnitOfWork unitofwork = new EntityFrameworkContext())
            {
                IEmployeeRepository employeeRepository = new EmployeeRepository(unitofwork);
                if (!employeeRepository.ExistsUserName(model.UserName))
                {
                    employeeRepository.Insert(model);
                    unitofwork.Commit();
                    return new OperationResult(OperationResultType.Success, "用户注册成功.");
                }
                return new OperationResult(OperationResultType.NoChanged, "用户名已存在.");
            }
        }

        public OperationResult ChangeEmployeeByUserName(string _UserName, Model.Employee model)
        {
            using (IUnitOfWork unitofwork = new EntityFrameworkContext())
            {
                IEmployeeRepository employeeRepository = new EmployeeRepository(unitofwork);
                var employee = employeeRepository.GetEntityByUserName(_UserName);
                if (employee != null)
                {
                    employeeRepository.Update(employee);
                    unitofwork.Commit();
                    return new OperationResult(OperationResultType.Success, "修改成功.");
                }
                return new OperationResult(OperationResultType.NoChanged,"所提供的用户名不正确.");
            }
        }
    }
}
